package com.hk.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.News;
import com.hk.model.Product;
import com.hk.model.ProductMenu;
import com.hk.model.vo.Result;
import com.hk.service.IProductMenuService;
import com.hk.service.IProductService;
import com.hk.service.impl.ProductMenuServiceImpl;
import com.hk.service.impl.ProductServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: zhe
 * @Date: 2021/6/21 9:50
 * @Statement: 通过ID回显当前产品或修改当前产品
 */
@WebServlet("/product/ProductFindByIdOrUpdate")
public class ProductFindByIdOrUpdateServlet extends HttpServlet {

    IProductService iProductService = new ProductServiceImpl();

    IProductMenuService iProductMenuService = new ProductMenuServiceImpl();

    /**
     * 通过Id查询当前产品信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String id = Tool.nullToString(req.getParameter("id"));
        Product product = iProductService.findById(Tool.parseInt(id));
        PrintWriter out = resp.getWriter();
        String productJSON = JSONObject.toJSONString(product);
        out.println(productJSON);
        out.flush();
        out.close();

    }

    /**
     * 修改产品信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        Product product = gson.fromJson(reader, Product.class);
        if (product == null) {
            product = new Product();
        }
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (product.getId() <= 0) {
            result.setStatus(0);
            result.setMessage("error");
            String resultJSON = JSONObject.toJSONString(result);
            out.println(resultJSON);
            out.flush();
            out.close();
        }
        //        通过子类型Id查询父类型Id并进行赋值
        ProductMenu productMenu = iProductMenuService.findById(product.gettMenuId());
        product.setMenuId(productMenu.getParentId());
        int update = iProductService.update(product);
        if (update < 1) {
            result.setStatus(update);
            result.setMessage("error");
        } else {
            result.setStatus(update);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();


    }

}
